(function () {
    "use strict";
    angular.module('accountManage', [])
        .factory('AccountManageService', function() {
            return {
                visibility: false
            };
        })
        .directive('accountManage', function (LoginInfo, Promise, $http, urlPrefix, $rootScope, AccountManageService) {
            var SAVE_STATUS = {SAVE: "保存", SAVING: "正在保存...", SAVED: "保存成功", FAULT: "保存失败"};
            var confirm = false;
            var flag = false;
            return {
                restrict: 'A',
                scope: {},
                link: function (scope) {
                    scope.canChangePass = true;
                    scope.visibility = AccountManageService.visibility;
                    scope.saveStatus = SAVE_STATUS["SAVE"];
                    scope.pass = {oldPassword: null, newPassword: null, newPasswordConfirm: null};

                    scope.setVisibility = function (status) {
                        if (status == true) {
                            scope.visibility = status;
                        } else {
                            scope.visibility = status;
                            $rootScope.$emit('showBodyScroll', true);

                            scope.canChangePass = true;
                            scope.saveStatus = SAVE_STATUS['SAVE'];
                            scope.pass = {oldPassword: null, newPassword: null, newPasswordConfirm: null};
                        }
                    }
                    Promise.when(LoginInfo).then(function (loginInfo) {
                        var currentUser = loginInfo.currentUser;
                        scope.username = currentUser["NAME"];
                    });
                    scope.saveAccountSettings = function () {
                        if (scope.pass.oldPassword == null || scope.pass.newPassword == null || scope.pass.newPasswordConfirm == null
                            || scope.pass.oldPassword == '' || scope.pass.newPassword == '' || scope.pass.newPasswordConfirm == '') {
                            scope.errMsg = '表单字段不能为空';
                            return false;
                        }
                        if (scope.checkConfirm() == false) {
                            return false;
                        }
                        var oldpass = scope.pass["oldPassword"];
                        $http.get(urlPrefix + "checkOldPasswordValid.do?theForm.password=" + encodeURIComponent(oldpass)).then(function (rsp) {
                            console.log(rsp);
                            flag = JSON.parse(JSON.parse(rsp.data));
                            if (flag == false) {
                                scope.errMsg = '您输入的旧密码与当前用户密码不匹配';
                                scope.saveStatus = SAVE_STATUS["SAVE"];
                                return false;
                            } else if (flag == true) {
                                scope.saveStatus = SAVE_STATUS["SAVING"];
                                var param = "theForm.password=" + encodeURIComponent(scope.pass.newPassword);
                                $http.get(urlPrefix + "changePassword.do?" + param).success(function () {
                                    scope.saveStatus = SAVE_STATUS["SAVED"];
                                });
                                scope.canChangePass = false;
                                return true;
                            } else {
                                scope.errMsg = SAVE_STATUS["FAULT"];
                            }
                        });
                    }
                    scope.checkConfirm = function () {
                        var newpass = scope.pass["newPassword"];
                        var confirmpass = scope.pass['newPasswordConfirm'];
                        if (newpass != confirmpass) {
                            scope.errMsg = '两次输入的密码不相同';
                            confirm = false;
                            return false;
                        } else {
                            scope.errMsg = null;
                            confirm = true;
                            return true;
                        }
                    };
                    $rootScope.$on('showAccountManageCenter', function (event) {
                        scope.visibility = true;
                        $rootScope.$emit('showBodyScroll', false);
                    });
                },
                replace: true,
                templateUrl: 'app/account/account-manage.html'
            }
        });
}());